package com.sanpu.kxe.business.main.presenter;

import android.graphics.Bitmap;
import android.view.View;

import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnItemClickListener;
import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.adapter.ShareAdapter;
import com.sanpu.kxe.business.main.bean.InviteFriendsOutput;
import com.sanpu.kxe.business.main.contract.InviteFriendsContract;
import com.sanpu.kxe.business.main.model.MainModel;
import com.sanpu.kxe.jump.shareutil.ShareUtil;
import com.sanpu.kxe.jump.shareutil.share.ShareListener;
import com.sanpu.kxe.jump.shareutil.share.SharePlatform;
import com.sanpu.kxe.utils.ToastUtil;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：邀请好友
 * Created by 薛勇军 on 2019/4/15.
 */

public class InviteFriendsPresenter implements InviteFriendsContract.Presenter {

    private InviteFriendsContract.View mView;

    private CompositeSubscription mSubscription;

    public InviteFriendsPresenter(InviteFriendsContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void inviteFriends() {
        mView.showLoadingDialog(true);
        Subscription subscription = MainModel.getInstance().inviteFriends()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<InviteFriendsOutput>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                        mView.showDataView(null);
                    }

                    @Override
                    public void onNext(List<InviteFriendsOutput> inviteFriendsOutputs) {
                        mView.showDataView(inviteFriendsOutputs);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void shareImageBitmap(final Bitmap bitmap) {
        final ShareAdapter mShareAdapter = new ShareAdapter(mView.getContext());
        DialogPlus dialog = DialogPlus.newDialog(mView.getContext())
                .setAdapter(mShareAdapter)
                .setHeader(R.layout.view_dialogplus_header)
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        if (position < 0) return;
                        prepareImage(mShareAdapter.getItem(position).getPlatform(), bitmap);
                    }
                })
                .setExpanded(false)  // This will enable the expand feature, (similar to android L share dialog)
                .create();
        dialog.show();
    }

    /**
     * 分享图片bitmap
     */
    private void prepareImage(final @SharePlatform.Platform int module, Bitmap mClipScreen) {
        ShareListener shareListener = new ShareListener() {
            @Override
            public void shareSuccess() {
                ToastUtil.showTextViewPrompt("分享成功！");
            }

            @Override
            public void shareFailure(Exception e) {
                ToastUtil.showTextViewPrompt(e.getMessage());
            }

            @Override
            public void shareCancel() {
            }
        };
        ShareUtil.shareImage(mView.getContext(), module, mClipScreen, shareListener);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
